<?php $searchTerm = $_POST["searchTerm"]; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Results | #<?php echo $searchTerm ?></title>

<meta name="description" content="View our large range of horse saddles online | We have saddles for most disciplines including General Purpose | Dressage | Eventing | Working | Hunter and VSD. Buy or sell saddles online." />

<link href="css/main.css" rel="stylesheet" type="text/css" />


<style type="text/css">
body {
	background-image: url(images/background.jpg);
	background-repeat: repeat-x;
	background-position: center top;
	background-attachment: fixed;
	margin: 0px;
	padding: 0px;
}
</style>

<link href="css/saddleShop.css" rel="stylesheet" type="text/css" />
<link href="css/searchResults.css" rel="stylesheet" type="text/css" />
</head>

<body onload="MM_preloadImages('images/facebookHover.png')">



<?php 
include "header.php"; 

//Add to database for history record
$host="localhost"; // Host name
$username="z247s504_admin"; // Mysql username
$password="PxLpCd07"; // Mysql password
$database="z247s504_website"; // Mysql Database

$currentDate = date('y,m,d');
$ipAddress = $_SERVER['REMOTE_ADDR'];

$con = mysql_connect($host,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db($database, $con);

mysql_query("INSERT INTO searchHistory 
	(
	searchTerm,
	searchDate,
	clientIP
	) 

VALUES 
	(
	'$searchTerm',
	'$currentDate',
	'$ipAddress'
	)
")

?>

<div id="content">

<?php 
//Add to database for history record
$host="localhost"; // Host name
$username="z247s504_admin"; // Mysql username
$password="PxLpCd07"; // Mysql password
$database="z247s504_website"; // Mysql Database

$con = mysql_connect($host,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db($database, $con);

?>

<?php  
  
////////////////////////
//Search Site PHP Code//
////////////////////////

$searchTerm = $_POST["searchTerm"]; //Get the string the user entered

$searchTerm = strtolower($searchTerm); //Converts the searched string into lowercase
   
$searchTermArray = explode(' ',$searchTerm); //Converts each searched word into an array value

$searchTermArray = array_unique($searchTermArray);

//$searchTerm = implode(',',$searchTerm);

$searchResults = array(); //Creates an array called searchResults

foreach ($searchTermArray as &$value) //For each value in the array preform the below
{
	
	$result = mysql_query("SELECT * FROM products WHERE productName LIKE '%$value%'");

	while($row = mysql_fetch_array($result))

  	{
		$productName = $row['productName']; //Save the product name as a variable
		$productName = strtolower($productName); //Convert the variable into lowercase
		$value = strtolower($value); //Convert the cvalue in the array to lower case
		$count = substr_count($productName, $value); //Count how many times the array value appears in the product name
		
		$searchDetail = 
		'<form action="productDetails.php" method="post" name="productDetails">
		<input name="productID" type="hidden" value="'.$row['productID'].'" />
 		<div id="productContainer">
		<h3>'.$row['productName'].'</h3>
		<p id="price">£'.$row['productCost'].'</p>
    	<p id="productDetailsSmall">'.$row['productDescription'].'</p>
    	<input id="viewButton" name="submit" type="submit" value="View Saddle" />
    	<img src="images/saddles/'.$row['productID'].'_1.jpg" width="500" height="438" />
		</div>
		</form>';
		
		
		array_push($searchResults,"$searchDetail"); //Add the product name found into the searchResults array
		
    }

}

//$searchResults = array_unique($searchResults);

//print_r($searchResults);
$countSearchResults = array_count_values($searchResults); //Counts the number of times the array value is in the array
											 //Counts the number of times the searched word appears in hte product name

echo "search Results For ".$searchTerm."<br /><br />";

$finalResults = array();

foreach($countSearchResults as $key => $value) //Create array containting | COUNT & PRODUCT NAME

{
	
    //Add the value (Count) and (product name) into the COUNTSEARCHRESULTS array
    array_push($finalResults,"<i class='hidden'>".$value."</i>".$key." ".$value." Matches");
}

//print_r($searchTerm);

arsort($finalResults); //Sort the array Highest to Lowest | COUNT value

foreach($finalResults as $key => $value)
{
  echo $value;
}

//print_r($finalResults); //Display the array

//for ($i=0; $i <= 4; $i++)
//echo $finalResults[$i]."<br \>"; //prints the array elements
  
////////////////////////
//Search Site PHP Code//
////////////////////////  
  ?>
  
</div>

</div>
<!--Content #End-->

<?php include"footer.php" ?>


</body>
</html>